package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.RadioStation;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class RadioStationClassifyAdapter extends BaseAdapter {

    private RadioStation.DataEntity.DataInEntity dataInEntity;
    private ArrayList<RadioStation.DataEntity.DataInEntity> dataInEntities;
    private Context context;

    public RadioStationClassifyAdapter(Context context, ArrayList<RadioStation.DataEntity.DataInEntity> dataInEntities) {
        this.context = context;
        this.dataInEntities = dataInEntities;
    }

    @Override
    public int getCount() {
        return dataInEntities != null && dataInEntities.size() > 0 ? dataInEntities.size() : 0;
    }

    @Override
    public RadioStation.DataEntity.DataInEntity getItem(int position) {
        return dataInEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.radio_station_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        dataInEntity = getItem(position);
        viewHolder.radioStationTagName.setText(dataInEntity.getTag_name());
        viewHolder.radioStationListenCount.setText("试听热度: " + dataInEntity.getListen_count());
        Picasso.with(context).load(dataInEntity.getPic_url_240_200())
                .placeholder(R.mipmap.placeholder_disk_180)
                .error(R.mipmap.placeholder_disk_180)
                .into(viewHolder.radioStationItemImg);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'radio_station_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.radio_station_item_img)
        ImageView radioStationItemImg;
        @Bind(R.id.radio_station_tag_name)
        TextView radioStationTagName;
        @Bind(R.id.radio_station_listen_count)
        TextView radioStationListenCount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
